package com.pkx.cloud.fse7.dms.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.cloud.fse7.dms.organization.entity.Organization;
import com.pkx.cloud.fse7.dms.organization.feign.OrganizationFeign;
import com.pkx.cloud.fse7.dms.organization.repo.OrganizationMapper;
import com.pkx.cloud.fse7.dms.organization.service.OrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * 服务实现类
 *
 * @author chenwg
 * @since 2020-11-17
 */
@Service
@Slf4j
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {

    @Autowired
    private OrganizationFeign organizationFeign;
    @Value(value = "${pkx.Gid}")
    private String Gid;

    /**
     * 分页列表
     *
     * @param organization
     * @return
     */
    @Override
    public Page<Organization> pageList(Page page, Organization organization) {
        LambdaQueryWrapper<Organization> lambdaQueryWrapper = Wrappers.lambdaQuery(Organization.class);
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param organization
     * @return
     */
    @Override
    public List<Organization> listByEntity(Organization organization) {
        LambdaQueryWrapper<Organization> lambdaQueryWrapper = Wrappers.lambdaQuery(Organization.class);
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public Organization selectById(Long id) {
        return getById(id);
    }

    /**
     * 保存
     *
     * @param organization
     * @return
     */
    @Override
    @Async
    public Boolean saveOrganization(Organization organization) {
        organization.setId(null);
        organization.setGid(Gid);
        String add = organizationFeign.add(organization);
        log.info("组织新增返回：{}", add);
        return true;
    }

    /**
     * 修改
     *
     * @param organization
     * @return
     */
    @Override
    @Async
    public Boolean modify(Organization organization) {
        // 根据标识号查询记录
        LambdaQueryWrapper<Organization> eq = Wrappers.lambdaQuery(Organization.class)
                .eq(Organization::getRid, organization.getRid());
        Organization one = getOne(eq);
        if (Objects.isNull(one)) {
            // 如果该记录不存在 则新增
            saveOrganization(organization);
            return true;
        }
        // 修改
        one.setParentId(organization.getParentId());
        one.setName(organization.getName());
        String modify = organizationFeign.modify(one);
        log.info("组织修改结果：{}", modify);
        return true;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Async
    public Boolean deleteById(String id) {
        LambdaQueryWrapper<Organization> eq = Wrappers.lambdaQuery(Organization.class)
                .eq(Organization::getRid, id);
        Organization one = getOne(eq);
        if (Objects.isNull(one)) {
            return false;
        }
        String ridStr = new StringJoiner("=", "\"(", ")\"")
                .add("Rid")
                .add(new StringJoiner("", "\'", "\'").add(id).toString()
                )
                .toString();
        ;
        String s = organizationFeign.deleteFunc(ridStr);
        log.info("组织删除结果：{}", s);
        return true;
    }
}
